﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using NthDownload.Contracts;
using NthDownload.Contracts.Plugins.Downloads;
using NthDownload.DownloadPlugins.Http.ViewModels;

namespace NthDownload.DownloadPlugins.Http
{
    public class HttpDownloadUIService : IDownloadUIService
    {
        #region Class Members

        private readonly IDownloadFactory _factory;

        #endregion Class Members

        #region Constructor

        public HttpDownloadUIService(IDownloadFactory factory)
        {
            _factory = factory;
        }

        #endregion Constructor

        #region Methods

        public Task<List<IDownload>> ShowAddDownloads()
        {
            List<IDownload> downloads = new List<IDownload>();
            TaskCompletionSource<List<IDownload>> createTaskCompletionSource = new TaskCompletionSource<List<IDownload>>();

            //
            // Prompt for Source
            //
            HttpDownloadsAddViewModel httpDownloadsAddViewModel = new HttpDownloadsAddViewModel();

            if (httpDownloadsAddViewModel.NavigationService.ShowDialog(httpDownloadsAddViewModel, true) != ViewModelResults.Ok)
            {
                createTaskCompletionSource.SetResult(downloads);
                return createTaskCompletionSource.Task;
            }

            //
            // Check Destination folder
            //
            DirectoryInfo diDest = new DirectoryInfo(httpDownloadsAddViewModel.DestinationPath);

            if (!diDest.Exists)
            {
                createTaskCompletionSource.SetResult(downloads);
                return createTaskCompletionSource.Task;
            }

            //
            // Create downloads for all selected http nodes
            //
            Task task = new Task(() =>
            {
                foreach (HttpNode httpNode in httpDownloadsAddViewModel.RootNodes)
                {
                    foreach (HttpNode selectedHttpNode in httpNode.GetSelectedRecursively())
                    {
                        downloads.Add(_factory.CreateDownload(selectedHttpNode.Path, diDest.FullName));
                    }
                }

                createTaskCompletionSource.SetResult(downloads);
            });

            task.ContinueWith(t =>
            {
                if (t.Exception != null)
                {
                    createTaskCompletionSource.SetException(t.Exception);
                }
            });

            task.Start();

            return createTaskCompletionSource.Task;
        }

        public Task<IDownload> ShowEditDownload(IDownload download)
        {
            throw new NotImplementedException();
        }

        public void ShowSettings()
        {
            HttpSettingsViewModel httpSettingsViewModel = new HttpSettingsViewModel();
            httpSettingsViewModel.NavigationService.ShowDialog(httpSettingsViewModel, false);
        }

        #endregion Methods
    }
}
